<template>
  <div id="app">
    <div>
      <div class="search-container">
        <h2>搜索</h2>
        <div class="search-header">
          <el-input
            v-model="searchForm.keyword"
            class="search-container-input"
            placeholder="请输入关键字"
            clearable
            size="medium"
            type="text"
            @keyup.native="enterSearch($event)"
          />
          <el-button class="search-btn" type="primary" size="medium" icon="el-icon-search" @click="fetchData" />
        </div>
        <div class="search-result">
          <el-pagination
            class="pagination"
            background
            :current-page="pageNum"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
          <div v-for="item in data.hits.hits" :key="item._source.id">
            <div class="docs-search-title">
              <a :href="`/mysql-manual/${item._source.filename}`">{{ item._source.title }}</a>
            </div>
            <div v-if="item._source.filename !== ''" class="docs-search-uri">https://dev.mysql.com/doc/refman/8.0/en/{{ item._source.filename }}</div>
            <div class="docs-search-synopsis">{{ item._source.content }}</div>
          </div>
          <el-pagination
            class="pagination"
            background
            :current-page="pageNum"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'Search',
  data() {
    return {
      data: {
        hits: {
          hits: [
            {
              _source: {
                id: 1,
                title: '',
                filename: '',
                content: ''
              }
            }
          ],
          total: {
            value: 0
          }
        }
      },
      visible: false,
      searchForm: {
        keyword: ''
      },
      pageNum: 1,
      pageSize: 10,
      total: 1
    }
  },
  mounted() {
    const params = this.param2Obj(location.href)
    if (params.keyword) {
      this.searchForm.keyword = params.keyword
    }
    this.fetchData()
  },
  methods: {
    fetchData() {
      const options = {
        params: {
          from: this.pageNum,
          size: this.pageSize,
          keyword: this.searchForm.keyword
        }
      }
      this.$http.get(process.env.VUE_APP_BASE_API + 'mysql-manual/search', options).then((response) => {
        this.data = response.body
        this.total = response.body.hits.total.value
        console.log(this.total)
        console.log(this.data)
        this.data.hits.hits.map(value => {
          value._source.content = value._source.content.substring(0, 150)
          value._source.content = value._source.content + '......'
        })
      })
    },
    enterSearch(event) {
      if (event.keyCode === 13) {
        this.fetchData(this.pageNum, this.pageSize, this.searchForm)
      }
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchData(this.pageNum, val)
    },
    handleCurrentChange(val) {
      console.log(val)
      this.pageNum = val
      this.fetchData(val, this.pageSize)
    },
    param2Obj(url) {
      const search = url.split('?')[1]
      if (!search) {
        return {}
      }
      return JSON.parse(
        '{"' +
        decodeURIComponent(search)
          .replace(/"/g, '\\"')
          .replace(/&/g, '","')
          .replace(/=/g, '":"') +
        // .replace(/\+/g, ' ') +
        '"}'
      )
    }

  }
}
</script>
<style lang="scss">
  .search-container{
    width: 800px;
    margin: 0 auto;
    .search-container-input {
      float: left;
      width: 40%;
    }
    .search-btn{
      margin-left: 5px;
    }
    .search-result{
      font-family: Microsoft Yahei,Arial,"Material Icons Extended",stonefont,iknow-qb_share_icons,review-iconfont,mui-act-font,fontAwesome,tm-detail-font,office365icons,MWF-MDL2,global-iconfont,"Bowtie",myfont,sans-serif !important;
      text-align: left;
      margin-top: 5px;
      a:visited {
        color: #0074a3;
      }
      a:link {
        color: #0074a3;
      }
      a:hover {
        text-decoration: underline;
      }
      a {
        background: transparent;
        text-decoration: none;
        margin: 0;
        padding: 0;
        font-size: 100%;
        vertical-align: baseline;
        cursor: pointer;
      }
      .docs-search-uri {
        font-size: 85%;
        color: #666;
      }
      .docs-search-synopsis {
        font-size: 90%;
        padding-bottom: 20px;
        color: #333;
      }
    }
  }
</style>
